<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构名称" prop="tenantName">
        <el-input v-model="queryParams.tenantName" placeholder="请输入机构名称" clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
                   v-hasPermi="['manage:electronicVisa:query']"
        >搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!--        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"-->
        <!--          v-hasPermi="['sys-enterprise:add']">新增</el-button>-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list" ref="table">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="机构名称" align="center" prop="tenantName"/>
      <el-table-column label="合同总数" align="center" prop="contractTotalNumber"/>
      <el-table-column label="使用数量" align="center" prop="contractUsedNumber"/>
      <el-table-column label="剩余数量" align="center" prop="contractLeftNumber"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- TODO 添加按钮权限 -->
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)"
                     v-hasPermi="['sys-enterprise:edit']"
          >明细
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row,'1')"
                     v-hasPermi="['manage:electronicVisa:edit']"
          >添加
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row,'2')"
                     v-hasPermi="['manage:electronicVisa:edit']"
                     v-if="scope.row.contractLeftNumber > 0"
          >扣减
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAuthorize(scope.row)"
                     v-hasPermi="['manage:electronicVisa:edit']" v-if="scope.row.electronicVisaStatus == 0"
          >授权
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />
    <!--明细-->
    <detail v-model:value="detailVisible" :id="id"/>
    <!--添加/扣减操作-->
    <handle v-model="handleVisible" :visaId="visaId" :tenantId="tenantId" :type="handleType" :title="title"
            @success="getList"
    />
  </div>
</template>

<script>
import { listElectronicVisa, electronicVisaAuth } from '@/api/electronicVisa'
import detail from '@/views/electronicVisa/components/detail.vue'
import handle from '@/views/electronicVisa/components/handle.vue'

export default {
  name: 'electronicVisa-list',
  dicts: [
    'department_type'
  ],
  components: {
    detail,
    handle
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantName: undefined,
        provinceId: undefined,
        cityId: undefined,
        districtId: undefined,
        type: undefined
      },
      // 表单参数
      form: {},
      id: undefined,

      visaId: undefined,
      //机构id
      tenantId: undefined,
      //明细弹窗
      detailVisible: false,
      //操作类型 1 添加 2 扣减
      handleType: undefined,
      //添加/扣减 弹窗
      handleVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listElectronicVisa(this.queryParams)
        .then((response) => {
          this.list = response.rows
          this.total = response.total
          this.loading = false
        })
        .catch((err) => {
          this.loading = false
        })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.provinceId = undefined
      this.queryParams.cityId = undefined
      this.queryParams.districtId = undefined
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      if (type == 1) {
        this.title = '添加合同数量'
        this.handleType = 1
      } else if (type == 2) {
        this.title = '扣减合同数量'
        this.handleType = 2
      }
      this.visaId = row.visaId
      this.tenantId = row.tenantId
      this.handleVisible = true
    },
    // 明细
    handleDetail(row) {
      this.detailVisible = true
      // this.id = row.visaId
      this.id = row.tenantId
    },
    // 授权
    handleAuthorize(row) {
      this.$confirm('确认授权该机构？').then(() => {
        electronicVisaAuth(row.tenantId).then((response) => {
          this.$message.success(response.msg || '授权成功')
          this.getList()
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.info {
  padding: 10px 15px;
  margin: 15px 0;
  background: #e6f7ff;
  color: #1890ff;

  span {
    color: #565c5f;
  }

  .clear {
    margin-left: 20px;
    cursor: pointer;
  }
}
</style>
